﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KPIS.GERP.GHRMS.APP.App_UI
{
    public partial class ExternalPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page.DataBind();
            }
        }

        public string UrlWebGims
        {
            get
            {
                string strRedirectUrl = string.Format("../{0}{1}?", (string.IsNullOrEmpty(Request["Controller"]) ? "" : Request["Controller"] + "/"), Request["Action"]);

                int i = 0;
                foreach (string segment in Request.RawUrl.Split('&'))
                {
                    if (i++ == 0) { continue; }
                    string[] parts = segment.Split('=');
                    if (parts.Length > 0)
                    {
                        string key = parts[0].Trim(new char[] { '?', ' ' });
                        string val = parts[1].Trim();
                        if (key.ToLower() != "action" && key.ToLower() != "controller")
                        {
                            strRedirectUrl += key + "=" + val + "&";
                        }
                    }
                }


                string strServer = HttpContext.Current.Request.Url.Host;
                string strUrlPrefix = string.IsNullOrEmpty(ConfigurationSettings.AppSettings["SubSystem"]) ? "" : "/" + ConfigurationSettings.AppSettings["SubSystem"];

                // ReqestPort = 80 mean production server, not 80 = debuging
                string serverPath = HttpContext.Current.Request.Url.Port != 80 ? "http://localhost:60830/" : string.Format("http://{0}{1}/WEB.GIMS/", strServer, strUrlPrefix);

                var sAuthen = new GerpAuthenService.GerpAuthenService();
                string tokenKey = sAuthen.CreateOTP(new SsMng().EmpSeqI, true, strRedirectUrl);
                string strUrl = string.Format("{0}Home/SingleSigon?&tokenKey={1}", serverPath, tokenKey);
                return strUrl; 
            }
        }
    }
}